# -*- coding: utf-8 -*-

import datetime

from sqlalchemy import desc
from webhelpers.date import time_ago_in_words
from webhelpers.text import urlify

from app import db


class Article(db.Model):
    __tablename__ = 'articles'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    body = db.Column(db.Text)
    created = db.Column(db.DateTime, default=datetime.datetime.now)

    def __repr__(self):
        return '<Article %r>' % (self.id)

    @classmethod
    def all(cls):
        return cls.query.order_by(desc(cls.created)).all()

    @property
    def created_in_words(self):
        return time_ago_in_words(self.created)


    @classmethod
    def find_by_id(cls, id):
        return cls.query.filter(cls.id==id).first()


    @property
    def slug(self):
        return urlify(self.title)
